<script setup>
import { ref } from 'vue'
import { Edit, Delete } from '@element-plus/icons-vue'
import ChannelSelect from './components/ChannelSelect.vue'
import ArticleEdit from './components/ArticleEdit.vue'
import { artGetListService, artDeleteService } from '@/api/article'
import { formatTime } from '@/utils/format'
const articleList = ref([])
const total = ref(0)
const articleRef = ref()
const params = ref({
  pageNum: 1,
  pageSize: 7,
  cate_id: '',
  status: ''
})
const loading = ref(false)
const getArticleList = async () => {
  loading.value = true
  const res = await artGetListService(params.value)
  articleList.value = res.data.data.items
  total.value = res.data.data.total
  loading.value = false
}
getArticleList()
const onDeleteArticle = async (row) => {
  await ElMessageBox.confirm('确认删除该文章信息？', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })
  const res = await artDeleteService(row.id)
  ElMessage.success(res.data.msg)
  // ElMessage({ type: 'success', message: '删除成功' })
  getArticleList()
}
const onEditArticle = (row) => {
  articleRef.value.open(row)
  console.log(row)
}

const handleCurrentChange = (val) => {
  params.value.pageNum = val
  getArticleList()
}
const handleSizeChange = (val) => {
  params.value.pageNum = 1
  params.value.pagesize = val
  getArticleList()
}
const onSearch = () => {
  params.value.pageNum = 1
  getArticleList()
}
const onReset = () => {
  params.value.pageNum = 1
  params.value.cate_id = ''
  params.value.status = ''
  getArticleList()
}
const addArticle = () => {
  articleRef.value.open({})
}
const onSuccess = (val) => {
  if (val === 'add') {
    const lastPage = Math.ceil((total.value + 1) / params.value.pagesize)
    params.value.pageNum = lastPage
  }
  getArticleList()
}
</script>
<template>
  <page-container title="文章管理">
    <template #extra>
      <el-button @click="addArticle" type="primary">发布文章</el-button>
    </template>
    <el-form inline>
      <el-form-item label="文章分类">
        <ChannelSelect v-model="params.cate_id"></ChannelSelect>
      </el-form-item>
      <el-form-item label="发布状态">
        <el-select v-model="params.status" placeholder="请选择">
          <el-option label="草稿" value="0"></el-option>
          <el-option label="已发布" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">搜索</el-button>
        <el-button type="primary" @click="onReset">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" height="50vh" :data="articleList">
      <el-table-column width="400" label="文章标题">
        <template #default="{ row }">
          <el-link type="primary" :underline="false" @click="onEditArticle(row)">{{ row.title }} </el-link>
        </template>
      </el-table-column>
      <el-table-column label="分类" prop="cate">
        <template #default="{ row }">
          {{ row.cate }}
        </template>
      </el-table-column>
      <el-table-column label="发表时间" prop="update_datetime">
        <template #default="{ row }">
          {{ formatTime(row.update_datetime) }}
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="status">
        <template #default="{ row }">
          <el-tag v-if="row.status === 1">发布</el-tag>
          <el-tag v-else>草稿</el-tag>
        </template>
      </el-table-column>
      <el-table-column width="100" label="操作">
        <template #default="{ row }">
          <el-button @click="onEditArticle(row)" type="primary" circle plain :icon="Edit"></el-button>
          <el-button @click="onDeleteArticle(row)" type="danger" circle plain :icon="Delete"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="justify-content: flex-end; margin-top: 20px"
      v-model:current-page="params.pageNum"
      v-model:page-size="params.pagesize"
      :page-sizes="[1, 2, 3, 7]"
      background
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <ArticleEdit @success="onSuccess" ref="articleRef"></ArticleEdit>
  </page-container>
</template>
<style lang="scss" scoped></style>
